<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="idy" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>

<div class="profile-tab-container">
	<c:if test="${param.action eq 'upload'}">
		<div class="alert alert-success">
			<strong>Alright!</strong> file successfully uploaded.
		</div>
	</c:if>
	
	<security:authorize ifNotGranted="GUEST_ACCESS">
		<div>
			<form:form commandName="patient" id="file-upload-form" enctype="multipart/form-data">
				<spring:bind path="patient.*">
					<c:if test="${status.error}">
						<div class='alert alert-error form-error'>
							<strong>Oh snap!</strong><br />
							<form:errors path="*" />
						</div>
					</c:if>
				</spring:bind>
				<div id="upload-controls" class="well well-small">
					<strong><span class="oranged">Upload a file</span></strong><br/>
					<span class="muted">*You can only upload image files such as png and jpg.</span><br/>
					<input type="file" name="tempFile" accept="image/*" required/>
					<input type="submit" value="Upload" class="btn btn-primary btn-small"/>
				</div>
			</form:form>
		</div>
	</security:authorize>
	<c:if test="${empty patient.scannedFiles}">
		<div class="text-info">
			There are no files to be displayed.
		</div>
	</c:if>
	<div>
		<c:forEach items="${patient.orderedScannedFiles}" var="file">
			<div class="file-item" id="file-${file.id}">
				<strong><span class="oranged">${file.fileName}</span></strong>
				<div class="pull-right">
					<a href="${url_context}/patient/file.jspx?id=${file.id}" target="_blank" class="btn btn-mini"><i class="icon-fullscreen" title="Preview"></i></a>
					<a href="${url_context}/download-file.jspx?FileInfoId=${file.id}" class="btn btn-mini" title="Download"><i class="icon-download-alt"></i></a>
					<a class="btn btn-mini" onclick="deleteFile(${file.id})" title="Remove"><i class="icon-remove"></i></a>
				</div>
				<div>
					Uploaded last <fmt:formatDate value='${file.createDate}' pattern="MMMM dd yyyy - hh:mma"/>
				</div>
			</div>
		</c:forEach>
		<div class="clear"></div>
	</div>
</div>


<script type="text/javascript">
	$(document).ready(function(){
		$('.profile-tab-container').fadeIn(500);
		
		$('#view-form').click(function(){
			$('#upload-controls').slideDown();
			$(this).slideUp(function(){
				
			});
		});
	});
	
	function deleteFile(fileId){
		if(confirm("Are you sure you want to delete this file?")){
			window.location = "${url_context}/patient/file/delete.jspx?id="+fileId+"&patientId=${patient.id}";
		}
		
	}
	
	$("#file-upload-form").submit(function(e) {
		e.preventDefault();
		
		var args = {
			formName : 'file-upload-form',
			divId : 'profile-menu-tab-wrapper',
		};

		IDEYATECH.util.submitForm('${url_context}/patient/file/upload.jspx?&patientId=${patient.id}', args);
	});
	
	
</script>